package com.pc.util.rabbitmq;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

@Configuration
//@PropertySource("classpath:configuration.properties")
public class RabbitmqConnection {
	@Value("${rabbitmq.host:1}")
	private String host;
	@Value("${rabbitmq.port:1}")
	private Integer port;
	@Value("${rabbitmq.virtualHost:1}")
	private String virtualHost;
	@Value("${rabbitmq.userName:1}")
	private String userName;
	@Value("${rabbitmq.password:1}")
	private String password;
	
	public Connection getConn(){
		try{
			ConnectionFactory factory=new ConnectionFactory();
			
			factory.setHost(host);
			factory.setPort(port);
			factory.setVirtualHost(virtualHost);
			factory.setUsername(userName);
			factory.setPassword(password);
		
			//从工厂获取连接
			Connection conn=factory.newConnection();
			return conn;
		}catch(Exception e){
			System.out.println(e.getMessage());
			return null;
		}
		
	}
	
}
